package labox.innovation.gameserver.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Logger;

import labox.innovation.db.core.L2DatabaseFactory;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance; //import labox.innovation.gameserver.network.serverpackets.ShortCutInit;
import labox.innovation.gameserver.network.serverpackets.ShortCutDel;

/**
 * 快捷栏管理类,管理某一个玩家的快捷栏
 *
 * @version $Revision: 1.1.2.1.2.3 $ $Date: 2005/03/27 15:29:33 $
 */
public class ShortCuts {

	private FuzePcInstance _owner;

	private Map<Integer, L2ShortCut> _shortCuts = new TreeMap<Integer, L2ShortCut>();

	public ShortCuts(FuzePcInstance owner) {
		_owner = owner;
	}

	public Map<Integer, L2ShortCut> getAllShortCuts() {
		return _shortCuts;
	}

	/**
	 * 获得快捷方式对象
	 * @param slot 快捷栏的槽位
	 * @param page 快捷栏的页码
	 * @return
	 */
	public L2ShortCut getShortCut(int slot) {
		L2ShortCut sc = _shortCuts.get(slot);
		//验证一下道具快捷方式是否有效
		if (sc != null && sc.getType() == L2ShortCut.TYPE_ITEM) {
			if (_owner.getInventory().getItemByObjectId(sc.getId()) == null) {
				deleteShortCut(sc.getSlot(),true);
				sc = null;
			}
		}

		return sc;
	}

	public void registerShortCut(L2ShortCut shortcut) {
		L2ShortCut oldShortCut = _shortCuts.put(shortcut.getSlot(), shortcut);
	}

	/**
	 * 从快捷栏删除一个快捷方式
	 * @param slot 槽位
	 */
	public void deleteShortCut(int slot,boolean confirm) {
		L2ShortCut old = _shortCuts.remove(slot);
		if (old == null)
			return;
		if(confirm)
			_owner.sendPacket(new ShortCutDel(old));//发送给客户端删除一个快捷方式

	}

	/**
	 * 从快捷栏删除一个快捷方式
	 * @param objectId 道具的objectid
	 */
	public void deleteShortCutByObjectId(long objectId) {
		L2ShortCut toRemove = null;
		for (L2ShortCut shortcut : _shortCuts.values()) {
			if (shortcut.getType() == L2ShortCut.TYPE_ITEM && shortcut.getId() == objectId) {
				toRemove = shortcut;
				break;
			}
		}
		if (toRemove != null)
			deleteShortCut(toRemove.getSlot(),true);
	}

	/**
	 * 读取快捷方式
	 */
	public void restore() {
		_shortCuts.clear();
//		Connection con = null;
//
//		try {
//			con = L2DatabaseFactory.getInstance().getConnection();
//			PreparedStatement statement = con.prepareStatement("SELECT charId, slot, type, shortcut_id, level FROM character_shortcuts WHERE charId=?");
//			statement.setLong(1, _owner.getObjectId());
//
//			ResultSet rset = statement.executeQuery();
//
//			while (rset.next()) {
//				int slot = rset.getInt("slot");
//				int type = rset.getInt("type");
//				int id = rset.getInt("shortcut_id");
//
//				L2ShortCut sc = new L2ShortCut(slot, type, id);
//				_shortCuts.put(slot, sc);
//			}
//
//			rset.close();
//			statement.close();
//		} catch (Exception e) {
//			_log.warning("Could not restore character shortcuts: " + e);
//		} finally {
//			try {
//				con.close();
//			} catch (Exception e) {
//			}
//		}

		// verify shortcuts

		for (L2ShortCut sc : getAllShortCuts().values()) {
			if (sc.getType() == L2ShortCut.TYPE_ITEM) {
				if (_owner.getInventory().getItemByObjectId(sc.getId()) == null)
					deleteShortCut(sc.getSlot(),true);
			}
		}
	}
}
